//
//  IweGoodsDetailCtr.swift
//  eemall
//
//  Created by zhuxietong on 2018/8/31.
//  Copyright © 2018年 zhuxietong. All rights reserved.
//

import Foundation
import soapp
import AsyncDisplayKit
import Eelay

public class GoodRuleWebNode: WebCellNode {

    
    public override func loadWeb(model: NSMutableDictionary) {
        let urlStr = model["__model.refundurl",""]
        if let url = URL(string: urlStr)
        {
            let req = URLRequest(url: url)
            eeWebView.webView.load(req)
        }
    }
    
}




open class IweGoodsDetailCtr: GoodsDetailCtr{
    
    
    open override var header:CoversView{
        get{
            if let hd = __header{
                return hd
            }
            let hd = CoversView(frame:  CGRect(x: 0, y: 0, width: Swidth, height: (Swidth-TP.topHeight)))
            hd.mo.controller = self
            __header = hd
            return hd
        }
    }
    
    public let ruleWebView = EeWebView()
        
    open override var section3:NSMutableArray{
        get{
            switch contentMode {
            case .info:
                return websection.mutable_array
            case .comment:
                let cms = comments.mutable_array
                if self.comments.count > 0{
                    cms.add(["cellID":"moreComment","__model":mo.detail].mutable_dictionary)
                }
                else{
                    cms.add(["cellID":"emptyComment"].mutable_dictionary)
                }
                return cms
            case .rule:
                return [["cellID":"ruleWeb","reuseID":"ruleWeb","__model":mo.detail]].mutable_array
            }
        }
    }
    
    class ServerCell1: ServerCell {
        override func mo_model_reload() {
            super.mo_model_reload()
            separatorHeight = 0
        }
    }
    
    override open var selector: TP.selector{
        get{
            let sel:TP.selector = [
                "cellID<info>":InfoCell1.self,
                "cellID<server>":ServerCell1.self,
                "cellID<web>":GoodsWebNode.self,
                "cellID<comment>":CommentCell.self,
                "cellID<emptyComment>":EmptyCommentCell.self,
                "cellID<moreComment>":MoreCommentCell.self,
                "cellID<recGoods>":RecItemCell.self,
                "cellID<__blank>":WhiteCell.self,
                "tagName<img>":ImageCell.self,
                "<section2>":TextNodeCell.self,
                "cellID<ruleWeb>":GoodRuleWebNode.self
                ]
            return sel
        }
    }
    
    
    override open func viewDidLoad() {
        bar = IweSegbar()
        super.viewDidLoad()
    
        view.backgroundColor = ._background
        tableView.backgroundColor = ._background
    }
    
    open override func initBottomBar() -> EeBottomBar {
        return BuyBar1()
    }
    
    
    
    
    
//    override open func tableNode(_ tableNode: ASTableNode, nodeForRowAt indexPath: IndexPath) -> ASCellNode {
//        let node = super.tableNode(tableNode, nodeForRowAt: indexPath)
//
//        if let webNode = node as? GoodRuleWebNode{
//
//            if webNode.eeWebView == nil
//            {
//                webNode.eeWebView = self.ruleWebView
//                let urlStr = mo.detail["refundurl",""]
//                if let url = URL(string: urlStr)
//                {
//                    let req = URLRequest(url: url)
//                    webNode.eeWebView?.webView.load(req)
//                }
//            }
//        }
//        return node
//    }

    
    open override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 3{
            return 56
        }
        return  0
    }
    
    deinit {
        //        print("++++++finish GoodsDetailCtr")
    }
}

extension IweGoodsDetailCtr
{
    open class IweSegbar:GoodsInfoModeBar,EeActionDelegate{
        public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
            
        }
        
        public class Kit: RuleCell {
            let titleL = UILabel()
            let indicator = UIView()
            override public func addLayoutRules() {
                contentView.eelay = [
//                    [indicator,[ee.L.B.R],"3"],
                    [titleL,[ee.X.Y,[0,0]]],
                ]
                backgroundColor = .clear
                _ = titleL.ui.font11.center.text(color: ._foreground)
            }
            public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
                mo_model_reload()
            }
            override public func mo_model_reload() {
                titleL.text = mo.model["name",""]
                
                if mo.model[FK.selected,""] == FK.YES
                {
//                    indicator.backgroundColor = UIColor(shex: ___shop.hex2)
                    contentView.bsui.set(radius: 0, width: 1, color: ._foreground)
                    titleL.textColor = ._foreground
                }
                else{
                    contentView.bsui.set(radius: 0, width: 1, color: ._foregroundWeak)
                    titleL.textColor = ._foregroundWeak
//                    indicator.backgroundColor = UIColor.table_bg_color
                }
            }
        }
        
        public let listV:RuleView<Kit> = RuleView<Kit>()
        override open func addLayoutRules() {
            let line = UIView()
            eelay = [
                [listV,[ee.X.Y],"36"],
                //                [line,[ee.T.L.R],"\(TP.separatorHeight)"]
            ]
            line.backgroundColor = UIColor._separator
            
            backgroundColor = ._background
            listV.space = (8,8)
            listV.que_num = 2
            listV.limit_width = Swidth
            listV.item_height = 36
            listV.padding = [4,AppTheme.edge,8,AppTheme.edge]
            let content = ["options":
                ShopContentMode.segbar
                ].mutable_dictionary
            
            loadSegContent(contentObj: content)
            
            self.frame = [0,0,Swidth,55]
            
            listV.touchAction = {[weak self]
                cell,id,data in
                self?.reloadBar(data: data)
            }
            self.alpha = 0
            co_delay(delay: 0.3) {[weak self] in
                self?.appear()
            }
        }
        
        func appear() {
            weak var wself = self
            UIView.animate(withDuration: 0.3) {
                wself?.alpha = 1
            }
        }
        
        
        open func loadSegContent(contentObj:NSMutableDictionary){
            listV.mo.model = contentObj
        }
        
        public func reloadBar(data:NSMutableDictionary)  {
            if data[FK.selected,""] == FK.YES{
                return
            }
            let options = DataNode.path(node: "\(listV.options_node)", listV.mo.model, value: NSMutableArray())
            options.list { (one:NSMutableDictionary, i) in
                if one["id",""] == data["id",""]{
                    one[FK.selected] = FK.YES
                }
                else{
                    one[FK.selected] = FK.NO
                }
            }
            listV.mo_model_reload()
            if let mode = ShopContentMode(rawValue: data["id",""])
            {
                changeMode(mode)
            }
            
            
        }
        
    }

}

